package com.navi.loaddata.loadService.impl.Dcdata;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.mesEntity.SpcChartMShopEqpt;
import com.navi.loaddata.core.mesEntity.SpcDcResultData;
import com.navi.loaddata.core.mesEntity.SpcDcdata;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.*;
import com.navi.loaddata.core.util.JdbcUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import com.navi.loaddata.loadService.impl.Dcdata.array.ProcessDcDataServiceIARRAY;
import com.navi.loaddata.loadService.impl.Dcdata.array.ProcessDcDataServicePARRAY;
import com.navi.loaddata.loadService.impl.Dcdata.cell1.ProcessDcDataServiceICELL1;
import com.navi.loaddata.loadService.impl.Dcdata.cell1.ProcessDcDataServicePCELL1;
import com.navi.loaddata.loadService.impl.Dcdata.cell2.ProcessDcDataServiceICELL2;
import com.navi.loaddata.loadService.impl.Dcdata.cell2.ProcessDcDataServicePCELL2;
import com.navi.loaddata.loadService.impl.Dcdata.cf.ProcessDcDataServiceICF;
import com.navi.loaddata.loadService.impl.Dcdata.cf.ProcessDcDataServicePCF;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author jay
 */
@Service
public class ProcessDcDataServiceI {
    private static final Logger logger = LoggerFactory.getLogger(ProcessDcDataServiceI.class);

    @Autowired
    private ProcessDcDataServiceIARRAY processDcDataServiceARRAY;
    @Autowired
    private ProcessDcDataServiceICF processDcDataServiceCF;
    @Autowired
    private ProcessDcDataServiceICELL1 processDcDataServiceCELL1;
    @Autowired
    private ProcessDcDataServiceICELL2 processDcDataServiceCELL2;

    public EtlGlassV processDcData(EtlConfDModel etlConfD, SpcDcdata spcDcdata) throws Exception {
        EtlGlassV etlGlassV = new EtlGlassV();
        switch (etlConfD.getShop()){
            case "ARRAY":
                etlGlassV=processDcDataServiceARRAY.processDcData(spcDcdata);
                break;
            case "CF":
                etlGlassV=processDcDataServiceCF.processDcData(spcDcdata);
                break;
            case "OC":
                if("CELL1".equals(etlConfD.getCellType())){
                    etlGlassV=processDcDataServiceCELL1.processDcData(spcDcdata);
                }else if("CELL2".equals(etlConfD.getCellType())){
                    etlGlassV= processDcDataServiceCELL2.processDcData(spcDcdata);
                }
                break;

        }
        return etlGlassV;
    }

    public EtlGlassSummaryV processGlassSummary(EtlGlassV etlGlassV, String paramCollection, SpcDcResultData firstResult) {
        EtlGlassSummaryV etlGlassSummaryV=new EtlGlassSummaryV();
        try{
            etlGlassSummaryV.setStepId(etlGlassV.getStepId());
//            etlGlassSummaryV.setSubEquipId(etlGlassV.getSubEquipId());
            etlGlassSummaryV.setGlassId(etlGlassV.getGlassId());
            etlGlassSummaryV.setGlassEndTime(etlGlassV.getGlassEndTime());
            etlGlassSummaryV.setParamCollection(paramCollection);//TODO
            etlGlassSummaryV.setParamName(firstResult.getItemname());
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCESS_MES2ORA_PDS_GLASS_SUMMARY,e);
        }
        return etlGlassSummaryV;
    }

    public EtlResultV processResultData(EtlConfDModel etlConfD, SpcDcdata spcDc, String paramCollection, SpcDcResultData curResult) {
        EtlResultV result=new EtlResultV();
        try {

//            result.setSubEquipId(spcDc.getUnitname());
            result.setStepId(spcDc.getProcessoperationname());
            result.setGlassId(spcDc.getMaterialname());
            result.setGlassEndTime(spcDc.getEventtime());
            result.setParamCollection(paramCollection);
            result.setParamName(curResult.getItemname());
            result.setSiteName(curResult.getSitename());
            result.setParamValue(ProcessUtil.getDouble(curResult.getResult()));
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCESS_MES2ORA_PDS_GLASS_RESULT,e);
        }
        return result;
    }


    public EtlGlassSummaryV setSpcChartM(EtlGlassSummaryV etlGlassSummaryV, HashMap<String, SpcChartMShopEqpt> spcChartMMap) {
        if(null!=spcChartMMap.get(etlGlassSummaryV.getParamName())){
            SpcChartMShopEqpt chart=spcChartMMap.get(etlGlassSummaryV.getParamName());
            etlGlassSummaryV.setUsl(chart.getUsl());
            etlGlassSummaryV.setLsl(chart.getLsl());
            etlGlassSummaryV.setTarget(chart.getTarget());
            etlGlassSummaryV.setUcl(chart.getUcl());
            etlGlassSummaryV.setLcl(chart.getLcl());
            etlGlassSummaryV.setCl(chart.getCl());
        }

        return etlGlassSummaryV;
    }
}
